BlankClassSoDocsWillBeGenerated

version(D_Ddoc)
class BlankClassSoDocsWillBeGenerated

Examples

This example is for the NOT NULL constraint. The table in SQL can be created by $(D $(D sql CREATE TABLE People ( Id INTEGER NOT NULL PRIMARY KEY, LastName TEXT NOT NULL, FirstName TEXT NOT NULL, City TEXT ); ))

I will create the singular class (or row class). The singular class should include a dup method and the mixin KeyedItem. The columns in the singular class should have a private member with getters and setters. No plural class (or table class) is needed for this example.

In making this package, I have made the assumption that the private member begins with an underscore and the getters and setters have the same name as the private member except no beginning underscore. The keyed item must also have some Unique constraint and will not compile without one.

The setter method provided in KeyedItem should be used in your setters. This will check if you are setting it to the same value, if the value causes any check constraints to be violated and if everything goes well notifies the plural class (or table) of the changes.

1 import db_constraints;
2 
3 // this is what I call the singular class
4 // you can also think of it as a row in the database.
5 // it contains all of the columns and tells us which
6 // columns have which constraints
7 class Person
8 {
9     private int _Id;
10     // marking Id with not null and primary key
11     @NotNull @PrimaryKeyColumn
12     @property int Id()
13     {
14         return _Id;
15     }
16     @property void Id(int value)
17     {
18         setter(_Id, value);
19     }
20 
21     private string _LastName;
22     // marking LastName with not null
23     @NotNull
24     @property string LastName()
25     {
26         return _LastName;
27     }
28     @property void LastName(string value)
29     {
30         setter(_LastName, value);
31     }
32 
33     private string _FirstName;
34     // marking FirstName with not null
35     @NotNull
36     @property string FirstName()
37     {
38         return _FirstName;
39     }
40     @property void FirstName(string value)
41     {
42         setter(_FirstName, value);
43     }
44 
45     private string _City;
46     // not marking City with anything
47     @property string City()
48     {
49         return _City;
50     }
51     @property void City(string value)
52     {
53         setter(_City, value);
54     }
55 
56     this(int Id_, string LastName_, string FirstName_, string City_)
57     {
58         this._Id = Id_;
59         this._LastName = LastName_;
60         this._FirstName = FirstName_;
61         this._City = City_;
62         // do not forget to initialize the keyed item!
63         initializeKeyedItem();
64     }
65 
66     // the keyed item mixin will create all the necessary
67     // checks for you
68     mixin KeyedItem!();
69 }
70 
71 
72 import std.exception : assertNotThrown, assertThrown;
73 // I will make a single row with values for all columns
74 // this does not throw an exception because none of the
75 // check constraints are violated.
76 assertNotThrown!CheckConstraintException(new Person(1, "Hanks",
77                                                     "Robert", "New York"));
78 
79 // the next Person throws a check constraint exception
80 // since we try to make a Person that has a null LastName
81 assertThrown!CheckConstraintException(new Person(2, null,
82                                                  "Marianne", "Chicago"));

Meta